package com.carcontrol.rotate.util;

import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;


import androidx.fragment.app.Fragment;

import com.carcontrol.rotate.base.BaseApplication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.HashMap;


/**
 * SharedPreferences存储数据方式工具类
 * 
 * @author lidongyao
 */
public class SharedPreferencesUrls {

	 private static SharedPreferencesUrls instance = new SharedPreferencesUrls();

	public SharedPreferencesUrls() {
	}

	private static synchronized void syncInit() {
		if (instance == null) {
			instance = new SharedPreferencesUrls();
		}
	}

	public static SharedPreferencesUrls getInstance() {
		if (instance == null) {
			syncInit();
		}
		return instance;
	}

	public android.content.SharedPreferences getSp() {
		return BaseApplication.getInstance().getSharedPreferences(BaseApplication.getInstance().getPackageName(), Context.MODE_PRIVATE);
	}

	public int getInt(String key, int def) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null)
				def = sp.getInt(key, def);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return def;
	}

	public void putInt(String key, int val) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
				e.putInt(key, val);
				e.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public long getLong(String key, long def) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null)
				def = sp.getLong(key, def);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return def;
	}

	public void putLong(String key, long val) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
				e.putLong(key, val);
				e.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getString(String key, String def) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null)
				def = sp.getString(key, def);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return def;
	}

	public void putString(String key, String val) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
				e.putString(key, val);
				e.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean getBoolean(String key, boolean def) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null)
				def = sp.getBoolean(key, def);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return def;
	}

	public void putBoolean(String key, boolean val) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
				e.putBoolean(key, val);
				e.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * desc:保存对象
	 * @param key
	 * @param obj 要保存的对象，仅仅能保存实现了serializable的对象
	 * modified:
	 */
	public void putObject(String key , Object obj){
		try {
			// 保存对象
//			SharedPreferences.Editor sharedata = context.getSharedPreferences(FILENAME, 0).edit();

			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
				//先将序列化结果写到byte缓存中，事实上就分配一个内存空间
				ByteArrayOutputStream bos=new ByteArrayOutputStream();
				ObjectOutputStream os=new ObjectOutputStream(bos);
				//将对象序列化写入byte缓存
				os.writeObject(obj);
				//将序列化的数据转为16进制保存
				String bytesToHexString = bytesToHexString(bos.toByteArray());
				//保存该16进制数组
				e.putString(key, bytesToHexString);
				e.commit();
			}


		} catch (Exception e) {
			e.printStackTrace();
			Log.e("", "保存obj失败");
		}
	}
	/**
	 * desc:将数组转为16进制
	 * @param bArray
	 * @return
	 * modified:
	 */
	public static String bytesToHexString(byte[] bArray) {
		if(bArray == null){
			return null;
		}
		if(bArray.length == 0){
			return "";
		}
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * desc:获取保存的Object对象
	 * @param key
	 * @return
	 * modified:
	 */
	public Object getObject(String key ){
		try {
//			SharedPreferences sharedata = context.getSharedPreferences(FILENAME, 0);

			android.content.SharedPreferences sp = getSp();
			if (sp != null){
				if (sp.contains(key)) {
					String string = sp.getString(key, "");
					if(TextUtils.isEmpty(string)){
						return null;
					}else{
						//将16进制的数据转为数组。准备反序列化
						byte[] stringToBytes = StringToBytes(string);
						ByteArrayInputStream bis=new ByteArrayInputStream(stringToBytes);
						ObjectInputStream is=new ObjectInputStream(bis);
						//返回反序列化得到的对象
						Object readObject = is.readObject();
						return readObject;
					}
				}
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			Log.e("", "读取obj失败");
		}

		return null;
	}

	/**
	 * desc:将16进制的数据转为数组
	 * @param data
	 * @return
	 * modified:
	 */
	public static byte[] StringToBytes(String data){
		String hexString=data.toUpperCase().trim();
		if (hexString.length()%2!=0) {
			return null;
		}
		byte[] retData=new byte[hexString.length()/2];
		for(int i=0;i<hexString.length();i++){
			int int_ch;  // 两位16进制数转化后的10进制数
			char hex_char1 = hexString.charAt(i); ////两位16进制数中的第一位(高位*16)
			int int_ch1;
			if(hex_char1 >= '0' && hex_char1 <='9')
				int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
			else if(hex_char1 >= 'A' && hex_char1 <='F')
				int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
			else
				return null;
			i++;
			char hex_char2 = hexString.charAt(i); ///两位16进制数中的第二位(低位)
			int int_ch2;
			if(hex_char2 >= '0' && hex_char2 <='9')
				int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
			else if(hex_char2 >= 'A' && hex_char2 <='F')
				int_ch2 = hex_char2-55; //// A 的Ascll - 65
			else
				return null;
			int_ch = int_ch1+int_ch2;
			retData[i/2]=(byte) int_ch;//将转化后的数放入Byte里
		}
		return retData;
	}


	public static String SceneList2String(HashMap<String, Fragment> hashmap)
			throws IOException {
		// 实例化一个ByteArrayOutputStream对象，用来装载压缩后的字节文件。
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		// 然后将得到的字符数据装载到ObjectOutputStream
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(
				byteArrayOutputStream);
		// writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
		objectOutputStream.writeObject(hashmap);
		// 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
		String SceneListString = new String(Base64.encode(
				byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
		// 关闭objectOutputStream
		objectOutputStream.close();
		return SceneListString;
	}

	@SuppressWarnings("unchecked")
	public static HashMap<String, Fragment> String2SceneList(
			String SceneListString) throws StreamCorruptedException,
            IOException, ClassNotFoundException {
		byte[] mobileBytes = Base64.decode(SceneListString.getBytes(),
				Base64.DEFAULT);
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
				mobileBytes);
		ObjectInputStream objectInputStream = new ObjectInputStream(
				byteArrayInputStream);
		HashMap<String, Fragment> SceneList = (HashMap<String, Fragment>) objectInputStream
				.readObject();
		objectInputStream.close();
		return SceneList;
	}

	public void putHashMap(String key, HashMap<String, Fragment> hashmap) {
//		SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
//		SharedPreferences.Editor editor = settings.edit();


		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
//				e.putBoolean(key, val);
//				e.commit();

				String liststr = SceneList2String(hashmap);
				e.putString(key, liststr);

				e.commit();
			}


		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public HashMap<String, Fragment> getHashMap(String key) {
//		SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);

		android.content.SharedPreferences sp = getSp();
		String liststr = sp.getString(key, "");
		try {
			return String2SceneList(liststr);
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void remove(String key) {
		try {
			android.content.SharedPreferences sp = getSp();
			if (sp != null) {
				Editor e = sp.edit();
				e.remove(key);
				e.commit();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
